/**
 * Created by sun on 2015/3/12.
 */
package com.jbm;

import com.sun.xml.internal.stream.writers.UTF8OutputStreamWriter;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

public class JSONEncoder extends ProtocolEncoderAdapter
{
	private final String charset;

	public JSONEncoder(String charset)
	{
		this.charset = charset;
	}

	public void encode(IoSession session, Object message,
	                   ProtocolEncoderOutput out) throws Exception
	{
		IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);
		String json = (String)message;
		byte[] bytes = json.getBytes("UTF-8");

		buf.putInt(bytes.length);
		buf.put(bytes);
		buf.flip();

		out.write(buf);
	}
}
